﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using TrainningCShape_Week01.DO;
using System.Xml;
using System.Data.SqlClient;

namespace TrainningCShape_Week01.DA
{
    class LoginIO
    {
       /*
        public static ArrayList LoadUsers()
        {
            // khai bao Arraylist de lu danh sach User
            ArrayList UserList = new ArrayList();
            // khai bao doi tuong XmlDocument
            XmlDocument doc = new XmlDocument();
            // doc file xml va luu vao doi tuong xmlDocument tren
            doc.Load(Directory.GetCurrentDirectory() + "\\XMLFile1.xml");

            //duyet qua tat ca cac no co ten la User
            foreach (XmlNode node in doc.GetElementsByTagName("User"))
            {
                string strUsername = "";
                string strPassword = "";



                // doi voi moi node User, duyet qua tat ca cac node con cua no
                foreach (XmlNode childNode in node)
                {
                    // neu node con co ten la Name, luu gia tri vao bienstrUsername
                    if (childNode.Name == "Name")
                    {
                        strUsername = childNode.InnerText;
                    }
                    // neu node con co ten la pass,luu gia tri vaof bien strPAsss
                    else if (childNode.Name == "Password")
                    {
                        strPassword = childNode.InnerText;
                    }
                }
                if (strUsername != "" && strPassword != "")
                {
                    // tao doi tuong Userinfo de luu thong tin cua 1 User
                    UserInfo user = new UserInfo(strUsername, strPassword);
                    // them user vao danh sach userlist
                    UserList.Add(user);
                }


            }
            // return
            return UserList;
        }
        */
        
        public static ArrayList  LoadUsers()
        {
            // Khai bao ArrayList de luu danh sach User
            ArrayList UserList = new ArrayList();
            // Khai bao chuoi ket noi
            // DataSource: database server
            // Database hoac Initial Catalog: ten database
            string strConn = "Data Source=MRCHAU-PC\\SQLEXPRESS;Database=QUANLYSINHVIEN;User ID=sa;Password=123456";
            // Tao doi tuong ket noi
            SqlConnection conn = new SqlConnection(strConn);
            // Tao doi tuong de Query du lieu
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            // conmand.ConmmanType = System.Data.CommandType.StoredProcedure;
            command.CommandText = "SELECT * FROM tbUser ";
            // Mo ket noi den database
            conn.Open();    
            // Doc du lieu tu database tung recoed (dong) mot
           SqlDataReader reader = command.ExecuteReader();
            
            while (reader.Read())
            {
                // tai moi dong co the lay truong du lieu can dung
                string strUsername = reader["Username"].ToString();
                string strPassword = reader["Password"].ToString();
                UserInfo user = new UserInfo(strUsername, strPassword);
                UserList.Add(user);

            }
            reader.Close();
            // dong ket noi
            conn.Close();
            // tra ket qua ve
             return UserList;
        
            
            }
        public static bool AddUsers(UserInfo user)
        {
            // Khai bao ArrayList de luu danh sach User
            // ArrayList UserList = new ArrayList();
            // Khai bao chuoi ket noi
            // DataSource: database server
            // Database hoac Initial Catalog: ten database
            string strConn = "Data Source=MRCHAU-PC\\SQLEXPRESS;Database=QUANLYSINHVIEN;User ID=sa;Password=123456";
            // Tao doi tuong ket noi
            SqlConnection conn = new SqlConnection(strConn);
            // Tao doi tuong de Query du lieu
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            // conmand.ConmmanType = System.Data.CommandType.StoredProcedure;
            command.CommandText = "INSERT INTO tbUser(Username,Password)" + "VALUES('" + user.Username + "','" + user.Password + "')";
            // Mo ket noi den database
            conn.Open();
            // Doc du lieu tu database tung recoed (dong) mot
            // SqlDataReader reader = 
            int count = command.ExecuteNonQuery();
            /*
            while (reader.Read())
            {
                // tai moi dong co the lay truong du lieu can dung
                string strUsername = reader["Username"].ToString();
                string strPassword = reader["Password"].ToString();
                UserInfo user = new UserInfo(strUsername, strPassword);
                UserList.Add(user);

            }
            reader.Close();
             * */
            // dong ket noi
            conn.Close();
            // tra ket qua ve
            if (count > 0)
            {
                return true;
            }
            else
                return false;


        }
    }
}